<template>
	<div>
		<div class="header">
			<van-cell size="large">
				<div slot="icon" class="iconko" @click="fanhui">
					<van-icon name="arrow-left" />
				</div>
				<div slot="title" class="cellbox">添加地址</div>
			</van-cell>

		</div>
		<van-address-edit show-postal :show-delete="ist" show-set-default show-search-result :search-result="searchResult"
		 @save="onSave" @delete="onDelete" @change-detail="onChangeDetail" disable-area :show-postal="jude" :show-area="jude"
		 :address-info="AddressInfo" @change-default="onChange" />
	</div>
</template>

<script>
	import {
		AddressEdit
	} from 'vant';
	import {
		Toast
	} from 'vant';
	import {
		Cell,
		CellGroup
	} from 'vant';
	import {
		Icon
	} from 'vant';
	export default {
		data() {
			return {
				jude: false,
				searchResult: [],
				AddressInfo: {},
				ist: true,
				eId:''
			};
		},
		created() {
			// 页面加载结束查看如果路由没有传递参数说明不是从编辑地址进入的所以不显示删除按钮
			if (!this.$route.params.id) {
				this.ist = false
			}
			//编辑内容，请求全部地址数据
			this.$get(this.$API.USERS_CONTECT, 
				 {
					userId: this.$store.state.nameId.userid
				}
			).then(res => {
				console.log(res)
				console.log((res.data[0].contact_flag))
				for (let i = 0; i < res.data.length; i++) {
					// 将默认值转为boolean值
					let bol = Boolean(res.data[i].contact_flag)
					if (res.data[i].id == this.$route.params.id) {
						this.AddressInfo = {
							name: res.data[i].name,
							tel: res.data[i].mobile,
							addressDetail: res.data[i].address,
							isDefault: bol //默认值
						}
					}
				}

			}).catch(err => {
				console.log(err)
			})
		},
		methods: {
			onSave(e) {
				// this.$router.push({path:'/address'})
				this.eId=e
				// 更新地址
				if (this.$route.params.id) {
					let isDe = Number(e.isDefault)
					this.$get(this.$API.USER_UPDATE_CONTECT, 
						{
							userId: this.$store.state.nameId.userid,
							name: e.name, //联系人
							mobile: e.tel, //电话号码
							contactFlag: isDe, //默认值
							address: e.addressDetail, //地址
							id: this.$route.params.id //收货地址id
						}
					).then(res => {
						console.log(res)
						Toast.success({
							message: '更新成功',
							duration: 700
						});
						this.$router.push({path:'/address'})
						// this.$router.go(-1)

					})
					console.log(isDe)

				} else {
					// 请求添加地址数据
					let isDe = Number(e.isDefault)
					this.$get(this.$API.UAERS_ADD_CONTECT, 
					{
							userId: this.$store.state.nameId.userid,
							name: e.name, //联系人
							mobile: e.tel, //电话号码
							contactFlag: isDe, //默认值
							address: e.addressDetail, //地址
					}).then(res => {
						console.log(res)
						if (res.status == 200) {
							Toast.success({
								message: '添加地址成功',
								duration: 700
							});
							this.$router.push({
								path: '/address'
							})
							// this.$router.go(-1)
						}


					}).catch(err => {
						console.log(err)
					})
				}

				console.log(e)
			},
			// 监听默认地址
			onChange(a) {
				this.aId=Number(a)
				if (Number(a) == 1 ) {
					this.$get(this.$API.USERS_CONTECT,
						 {
							userId: this.$store.state.nameId.userid
						}
					).then(res => {
						for(let i=0;res.data[i];i++){
							console.log(res.data[i].contact_flag)
						if(res.data[i].contact_flag==1){ 
							 this.$axios.get('http://116.63.80.218/users/update/contect', {
							 	params: {
							 		userId: this.$store.state.nameId.userid,
							 		name:res.data[i].name, //联系人
							 		mobile:res.data[i].mobile, //电话号码
							 		contactFlag:0, //默认值
							 		address: res.data[i].address, //地址
							 		id:res.data[i].id// 所有的收货地址id
							 	}
							 }).then(res => {
							 	console.log(res)
 
							 })
						 }
						}  
					})
				}
			},
			onDelete() {
				// 判断如果路由有东西传过来那么就是点击编辑按钮过来的
				if (this.$route.params.id) {
					this.$get(this.$API.USERS_REMOVE_CONTECT, 
						{
							userId: this.$store.state.nameId.userid,
							id: this.$route.params.id
						}
					).then(res => {
						console.log(res)
						// this.ist=true
						Toast.success('删除成功');
						this.$router.go(-1)
					}).catch(err => {
						console.log(err)
					})
				} else {

				}
			},
			onChangeDetail(val) {
				if (val) {
					this.searchResult = [{
						name: '黄龙万科中心',
						address: '杭州市西湖区',
					}, ];
				} else {
					this.searchResult = [];
				}
			},

			fanhui() {
				this.$router.go(-1)
			}
		},
		components: {
			[AddressEdit.name]: AddressEdit,
			[Toast.name]: Toast,
			[Cell.name]: Cell,
			[CellGroup.name]: CellGroup,
			[Icon.name]: Icon,
		}
	}
</script>

<style lang="less" scoped>
	.header .van-cell {
		box-shadow: 0 1px 1px rgba(100, 100, 100, 0.2);

	}

	.cellbox {
		font-size: 15px;
		text-align: center;
	}

	.iconko {
		font-size: 20px;
	}

	/deep/.van-button--danger {
		color: #fff;
		background-color: #343434;
		border: 1px solid #343434;
	}

	/deep/.van-button--default {
		color: #323233;
		margin-top: 20px;
	}

	/deep/.van-switch--on {
		background-color: #343434;
	}
</style>
